<template>
    <transition>
        <div class="efomo-main-box">
            <!--内容-->
            <div class="efomo-main-content">
                <!--每周抽奖-->
                <h5 class="efomo-h5">{{$t('m.efomo.lottery.tit')}} <span
                        @click="aShowTip({id:'4',explainShow:true})">?</span></h5>
                <div class="lootery-time">
                    <em>{{$t('m.efomo.lottery.time')[0]}}
                        <span v-if="$parent.wCD.time > 0">
                        {{ $parent.wCD.h }}:{{ $parent.wCD.m }}:{{ $parent.wCD.s }}
                        </span>
                        <span v-else>
                            {{$t('m.efomo.lottery.time')[2]}}
                        </span>
                        {{$t('m.efomo.lottery.time')[1]}}</em>
                </div>
                <div class="efomo-tit-asset efomo-lootery-tit">
                    <span>{{ fomoInfo.weekPot }}</span>ETSC
                </div>
                <div class="efomo-main-btn lottery-btn">
                    <button v-if="fomoInfo.wID > 1" @click="aShowList({id:1,listShow: true})">
                        <span>{{$t('m.efomo.btn.lottery')[0]}}</span>
                    </button>
                    <button v-else style="background-color: rgb(151, 152, 154);border-color: rgb(151, 152, 154);">
                        <span>{{$t('m.efomo.btn.lottery')[0]}}</span>
                    </button>

                    <!--获取周抽奖-->
                    <button v-if="canGetWeekAward" @click="getWeekAwardPre()">
                        <span>{{$t('m.efomo.btn.lottery')[1]}}</span>
                    </button>
                    <button v-else style="background-color: rgb(151, 152, 154);border-color: rgb(151, 152, 154);">
                        <span>{{$t('m.efomo.btn.lottery')[1]}}</span>
                    </button>
                </div>
                <!--最终抽奖-->
                <h5 class="efomo-h5" style="padding-top: 26px;">{{$t('m.efomo.lottery.subtit')}} <span
                        @click="aShowTip({id:'0',explainShow:true})">?</span></h5>
                <div class="efomo-main-btn lottery-btn" style="margin-top: 20px;">
                    <button v-if="fomoInfo.lID > 1" @click="aShowList({id:2,listShow: true})">
                        <span>{{$t('m.efomo.btn.lottery')[0]}}</span>
                    </button>
                    <button v-else style="background-color: rgb(151, 152, 154);border-color: rgb(151, 152, 154);">
                        <span>{{$t('m.efomo.btn.lottery')[0]}}</span>
                    </button>

                    <!--获取最终抽奖-->
                    <button v-if="canGetLastAward" @click="getLastAwardPre()">
                        <span>{{$t('m.efomo.btn.lottery')[1]}}</span>
                    </button>
                    <button v-else style="background-color: rgb(151, 152, 154);border-color: rgb(151, 152, 154);">
                        <span>{{$t('m.efomo.btn.lottery')[1]}}</span>
                    </button>
                </div>
                <div class="efomo-main-tip" :class="weekAwardList.length == 0?'max-box':''">
                    <p>{{$t('m.efomo.tips')[2]}}</p>
                </div>
            </div>
            <!--底部列表-->
            <div v-show="weekAwardList.length > 0" class="efomo-main-foot">
                <div class="efomo-main-list">
                    <h5>{{$t('m.efomo.list.tit')[2]}}</h5>
                    <ul class="efomo-list-type efomo-lootery-list">
                        <li>{{$t('m.efomo.list.type')[0]}}</li>
                        <li>{{$t('m.efomo.list.type')[3]}}</li>
                        <li>{{$t('m.efomo.list.type')[5]}}</li>
                    </ul>
                    <ul v-for="v in weekAwardList" class="efomo-list-item efomo-lootery-list">
                        <li>{{ v.timestamp | time2DateFomo }}</li>
                        <li>{{$t('m.efomo.success')[1]}}</li>
                        <li class="price">{{ v.eth }}</li>
                    </ul>
                    <ul v-for="v in lastAwardList" class="efomo-list-item efomo-lootery-list">
                        <li>{{ v.timestamp | time2DateFomo }}</li>
                        <li>{{$t('m.efomo.success')[1]}}</li>
                        <li class="price">{{ v.eth }}</li>
                    </ul>
                </div>
            </div>
        </div>
    </transition>
</template>

<script>
    import '@/assets/css/efomo.scss'
    import axios from 'axios';
    import {getWeekAward, getLastAward} from '@/assets/js/etherjs/fomo/fomo'
    import {myAlert} from "@/assets/js/h5plus/nativeUI";
    import {AESDecrypt} from '@/assets/js/utils/crypto'

    export default {
        name: "efomo_lottery",
        data() {
            return {
                pathName: 'lottery',
                weekAwardList: [],
                lastAwardList: [],
                request: {
                    pageNo: 1,
                    pageSize: 50,
                    address: "",
                    isEnd: false,
                },
                canGetWeekAward: false, // week抽奖按钮是否可用
                canGetLastAward: false  // last抽奖是否可用
            }
        },
        mounted() {
            // 设置title
            this.$parent.title = this.$t('m.efomo.title')[3];
            this.$parent.init();
        },
        watch: {
            prompt(v) {
                if (v.isConfirm) {
                    let pKey = AESDecrypt({
                        pwd: v.value,
                        msg: this.walletActive.pKey
                    });
                    if (pKey == '') {
                        myAlert({
                            msg: this.$t('m.wallet.manager.index.prompt[7]'),
                            button: this.$t('m.wallet.manager.index.prompt[4]')
                        });
                        return
                    }
                    this.aWalletDecode({
                        pKey: pKey
                    });
                    if (v.complete == 'getWeekAward') {
                        // computed计算到的变化，不及时？
                        this.getWeekAward();
                    }
                    if (v.complete == 'geLastAward') {
                        // computed计算到的变化，不及时？
                        this.geLastAward();
                    }
                }
            }
        },
        methods: {
            async init() {
                this.getWeekWinner();
                this.getWeekAwardList();

                this.getLastWinner();
                this.getLastAwardList();
            },
            changeTab(url, path) { //路由跳转
                this.active = path;
                this.changeRoute(url);
            },

            /////////////////////////周抽奖部分/////////////////////////
            /*
             * 获取周中奖玩家列表
             */
            getWeekWinner() {
                if (this.fomoInfo.wID < 2) return new Promise((resolve) => {
                    resolve(1);
                });
                return axios.get(this.requestConfig.apiHost + "/client/fomo/week/winners", {
                    params: {
                        ID: this.fomoInfo.wID - 1,
                        type: 1
                    }
                }).then((response) => {
                    if (response.data.error_code != 0) {
                        throw new Error("get list error");
                    }
                    let list = response.data.data.list;
                    this.aWeekWinnerList(list);
                    // 判断是否可以点击获奖按钮
                    for (let i in list) {
                        if (list[i].address == this.walletActive.address) {
                            let key = "weekAward_" + this.walletActive.address + "_" + (this.fomoInfo.wID - 1);
                            let isGet = localStorage.getItem(key);
                            if (!isGet) {
                                this.canGetWeekAward = true;
                            }
                        }
                    }
                }).catch((error) => {
                    myAlert({
                        msg: "获取数据失败",
                        button: this.$t('m.wallet.transaction.list.prompt[9]')
                    });
                    console.log(error);
                });
            },
            /*
             * 获取周领取奖励列表
             */
            getWeekAwardList() {
                if (this.fomoInfo.wID < 2) return new Promise((resolve) => {
                    resolve(1);
                });
                return axios.get(this.requestConfig.apiHost + "/client/fomo/week/award", {
                    params: {
                        // wID: this.fomoInfo.wID - 1,
                        address: this.walletActive.address
                    }
                }).then((response) => {
                    if (response.data.error_code != 0) {
                        throw new Error("get list error");
                    }
                    this.weekAwardList = response.data.data.list;
                }).catch((error) => {
                    myAlert({
                        msg: this.$t('m.efomo.toast[15]'),
                        button: this.$t('m.wallet.transaction.list.prompt[9]')
                    });
                    console.log(error);
                });
            },
            getWeekAwardPre() {
                if (this.walletDecode.pKey == "") {
                    this.aPrompt({
                        show: true,
                        title: this.$t('m.my.identity.prompt[7]'),
                        cancel: this.$t('m.my.identity.prompt[8]'),
                        confirm: this.$t('m.my.identity.prompt[6]'),
                        type: "password",
                        placeholder: this.$t('m.my.identity.prompt[7]'),
                        complete: "getWeekAward"
                    })
                } else {
                    this.getWeekAward();
                }
            },
            async getWeekAward() {
                console.log("正在获取奖励");
                this.plusDiff(() => {
                    plus.nativeUI.toast(this.$t('m.efomo.toast[16]'), {verticalAlign: 'center'});
                });
                let res = await getWeekAward({
                    privateKey: this.walletDecode.pKey,
                    wID: this.fomoInfo.wID - 1
                });
                if (res == 0) {
                    console.log("获取奖励失败");
                    this.plusDiff(() => {
                        plus.nativeUI.toast(this.$t('m.efomo.toast[5]'), {verticalAlign: 'center'});
                    });
                } else {
                    // 先刷新 fomoInfo数据
                    console.log("获取奖励成功");
                    this.plusDiff(() => {
                        plus.nativeUI.toast(this.$t('m.efomo.toast[6]'), {verticalAlign: 'center'});
                    });
                    this.canGetWeekAward = false;
                }
            },

            /////////////////////////最终抽奖部分///////////////////////
            /*
             * 获取last中奖玩家列表
             */
            getLastWinner() {
                if (this.fomoInfo.lID < 2) return new Promise((resolve) => {
                    resolve(1);
                });
                return axios.get(this.requestConfig.apiHost + "/client/fomo/week/winners", {
                    params: {
                        ID: this.fomoInfo.lID - 1,
                        type: 0
                    }
                }).then((response) => {
                    if (response.data.error_code != 0) {
                        throw new Error("get list error");
                    }
                    let list = response.data.data.list;
                    this.aLastWinnerList(list);
                    // 判断是否可以点击获奖按钮
                    for (let i in list) {
                        if (list[i].address == this.walletActive.address) {
                            let key = "lastAward_" + this.walletActive.address + "_" + (this.fomoInfo.lID - 1);
                            let isGet = localStorage.getItem(key);
                            if (!isGet) {
                                this.canGetLastAward = true;
                            }
                        }
                    }
                }).catch((error) => {
                    myAlert({
                        msg: "获取数据失败",
                        button: this.$t('m.wallet.transaction.list.prompt[9]')
                    });
                    console.log(error);
                });
            },
            /*
             * 获取last领取奖励列表
             */
            getLastAwardList() {
                if (this.fomoInfo.lID < 2) return new Promise((resolve) => {
                    resolve(1);
                });
                return axios.get(this.requestConfig.apiHost + "/client/fomo/last/award", {
                    params: {
                        // lID: this.fomoInfo.lID - 1,
                        address: this.walletActive.address
                    }
                }).then((response) => {
                    if (response.data.error_code != 0) {
                        throw new Error("get list error");
                    }
                    this.lastAwardList = response.data.data.list;
                }).catch((error) => {
                    myAlert({
                        msg: this.$t('m.efomo.toast[15]'),
                        button: this.$t('m.wallet.transaction.list.prompt[9]')
                    });
                    console.log(error);
                });
            },
            getLastAwardPre() {
                if (this.walletDecode.pKey == "") {
                    this.aPrompt({
                        show: true,
                        title: this.$t('m.my.identity.prompt[7]'),
                        cancel: this.$t('m.my.identity.prompt[8]'),
                        confirm: this.$t('m.my.identity.prompt[6]'),
                        type: "password",
                        placeholder: this.$t('m.my.identity.prompt[7]'),
                        complete: "geLastAward"
                    })
                } else {
                    this.geLastAward();
                }
            },
            async geLastAward() {
                console.log("正在获取奖励");
                this.plusDiff(() => {
                    plus.nativeUI.toast(this.$t('m.efomo.toast[16]'), {verticalAlign: 'center'});
                });
                let res = await getLastAward({
                    privateKey: this.walletDecode.pKey,
                    lID: this.fomoInfo.lID - 1
                });
                if (res == 0) {
                    console.log("获取奖励失败");
                    this.plusDiff(() => {
                        plus.nativeUI.toast(this.$t('m.efomo.toast[5]'), {verticalAlign: 'center'});
                    });
                } else {
                    // 先刷新 fomoInfo数据
                    console.log("获取奖励成功");
                    this.plusDiff(() => {
                        plus.nativeUI.toast(this.$t('m.efomo.toast[6]'), {verticalAlign: 'center'});
                    });
                    this.canGetLastAward = false;
                }
            },

        }
    }
</script>

<style scoped>

</style>